import requests

def test_homepage():
    # 创建会话以保持cookies
    session = requests.Session()

    # 登录
    login_data = {
        'username': 'admin',
        'password': 'password'
    }
    login_response = session.post('http://localhost:5000/login', data=login_data)
    print(f"登录响应状态码: {login_response.status_code}")

    # 访问首页
    home_response = session.get('http://localhost:5000/home')
    print(f"首页状态码: {home_response.status_code}")

    # 保存首页内容到文件
    with open('homepage.html', 'w', encoding='utf-8') as f:
        f.write(home_response.text)
    
    print("首页内容已保存到 homepage.html")

if __name__ == "__main__":
    test_homepage()